﻿@model BackEnd.Models.SlideShowModel
@{
    ViewBag.Title = "Index";
}
<script>
    function ValidateData()
    {
        if ($('#iptTimeWaiting').val().length == 0)
        {
            alert('Pleas insert time waiting!');
            $('#iptTimeWaiting').focus();
            return;
        }
        strFile = '';
        arrayFile = $('#iphFileName').val().split(';')
        for (var i = 0; i < arrayFile.length -1; i++)
        {            
            strFile = strFile + arrayFile[i] + '#' + $('#RedirectUrl' + arrayFile[i].replace(/\./gi, "_")).val() + ";";
        }
        $.ajax({
            url: '/SlideShow/Create',
            type: 'POST',
            data: {                
                timeWaiting: $('#iptTimeWaiting').val(),
                file: strFile
            },
            success: function (data) {
                switch (data) {
                    case -1:
                        alert('Pleas insert time waiting!');
                        $('#iptTimeWaiting').focus();
                        break;
                    case -2:
                        window.location("/Account/Login");
                        break;
                    case -3:
                        window.location("/Account/Login");
                        break;
                    default:
                        alert('Insert Success!');
                }
            }
        });
    }

    function RemoveFileUpload(spanId,fileName) {
        $("#" + spanId).remove();
        $("#iphFileName").val($("#iphFileName").val().replace(fileName + ';', ''));        
    }
</script>
<h2>Slide Show</h2>
<table>
    <tr>
        <td>@Html.LabelFor(m=>m.TimeWaiting)</td>
        <td><input id="iptTimeWaiting" type="text" value="@Model.TimeWaiting" /></td>
    </tr>
    <tr>
        <td><label>Image</label></td>
        <td>
            <input type="file" id="ipfFile"/>            
                <script type="text/javascript">
                    $('#ipfFile').uploadify({
                    'script': '/SlideShow/UploadFile',
                    'uploader': '/Scripts/uploadify/uploadify.swf',
                    'buttonImg': '/Scripts/uploadify/btn_select.png',
                    'cancelImg': '/Scripts/uploadify/cancel.png',
                    'folder': '/SlideShow',
                    'width': '51',
                    'height': '23',
                    'fileExt': '*.jpg;*.gif;*.png;',
                    'fileDesc': '*.jpg;*.gif;*.png;',
                    'sizeLimit': '52428800',
                    'queueSizeLimit': 10,
                    'multi': true,
                    'auto': true,
                    'scriptData': { RequireUploadifySessionSync: true, SecurityToken: UploadifyAuthCookie, SessionId: UploadifySessionId },
                    'onQueueFull': function (event, queueID, fileObj) {
                    },
                    'onSelect': function (event, queueID, fileObj) {
                    },
                    'onError': function (event, queueID, fileObj, obj) {                        
                    },
                    'onProgress': function (event, queueID, fileObj, obj) {
                    },
                    'onComplete': function (event, queueID, fileObj, response, data) {
                        var json = eval("(" + response + ")");
                        if (json.errorCode == 100) {
                            $('#iphFileName').val($('#iphFileName').val()+json.fileName+";");
                            var spanId = json.fileName.replace(/\./gi, "_");
                            var str = "<span id=" + spanId + ">" + json.fileName + "<br/><input type='text' name='RedirectUrl' id='RedirectUrl" + spanId + "' /> (<a href=javascript:RemoveFileUpload('" + spanId + "','" + json.fileName + "')>Xóa</a>)<br/></span>";
                            $("#fileQueue").html($("#fileQueue").html() + str);
                        } else {                            
                        }
                    },
                    'onAllComplete': function () {
                    }
                });
            </script>
            
            <div id="fileQueue">
                @foreach (var item in Model.ListSlideShowDetail)
                { 
                    <span id="@item.Image.Replace(".","_")">@item.Image<br/><input type="text" name="RedirectUrl" id="RedirectUrl@(item.Image.Replace(".","_"))" value="@item.RedirectUrl" /> (<a href="javascript:RemoveFileUpload('@item.Image.Replace(".","_")','@item.Image')">Xóa</a>)<br /></span>
                }
            </div>
        </td>
    </tr>
    <tr>
        @{
            string listImage = "";
            foreach (var item in Model.ListSlideShowDetail)
            {
                listImage += item.Image + ";";
            }
        }
        <td><input type="hidden" id="iphFileName" value="@listImage"/></td>
        <td><input type="button" value="Submit" onclick="ValidateData()" /></td>
    </tr>
</table>
